<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8"/>
    <title>房子管理</title>
    <link href="${pageContext.request.contextPath}/static/images/houserent.ico" rel="icon">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/module/admin.css?v=318" media="all"/>
</head>
<body onscroll="layui.admin.hideFixedEl();" class="theme-pink close-footer">
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form toolbar table-tool-mini">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索:</label>
                        <div class="layui-input-block">
                            <select name="houseStatus" lay-verType="tips" lay-verify="required" required>
                                <option value="">请选择状态</option>
                                <option value="3">已预订</option>
                                <option value="4">已租赁</option>
                                <option value="5">已退租</option>
                                <option value="6">申请退租</option>
                                <option value="7">未租赁</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" lay-filter="searchLand" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <table id="landHome" lay-filter="landHome"></table>
        </div>
    </div>
</div>
<script type="text/html" id="houseEdit">
<form class="layui-form" id="landHouseInfo" lay-filter="landHouseInfo" style="width: 400px;margin: 0 auto;padding: 20px;">
    <input type="hidden" name="houseId" class="layui-input"/>
    <input type="hidden" name="parentId" class="layui-input"/>
    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">房屋名:</label>
        <div class="layui-input-block">
            <input name="houseName" placeholder="请输入房屋名" class="layui-input"
                   lay-verType="tips" lay-verify="required" required/>
        </div>
    </div>
    <input type="hidden" name="houseStatus" value="7">
    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">月租金:</label>
        <div class="layui-input-block">
            <input type="number" name="housePrice" placeholder="请输入月租金" class="layui-input"
                   lay-verType="tips" lay-verify="required|number" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">房产证号:</label>
        <div class="layui-input-block">
            <input name="houseIdCard" placeholder="请输入房产证号" class="layui-input"
                   lay-verType="tips" lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">房屋地址:</label>
        <div class="layui-input-block">
            <input name="houseAddress" placeholder="请输入房屋地址" class="layui-input"
                   lay-verType="tips" lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">租赁类型:</label>
        <div class="layui-input-block">
            <select name="houseType" lay-verType="tips" lay-verify="required" required>
                <option value="">请选择租赁类型</option>
                <option value="8">整租</option>
                <option value="9">合租</option>
            </select>
        </div>
    </div>
    <div class="layui-upload">
        <button type="button" class="layui-btn" id="houseImgs">上传房屋图片(可选择多张图片)</button>
        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
            预览图：
            <div class="layui-upload-list" id="houseImg"></div>
        </blockquote>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-filter="landHouseInfo" lay-submit style="background-color: #009688;">&emsp;提交&emsp;</button>
        </div>
    </div>
</form>
</script>

<script type="text/html" id="landHomeTar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="check">查看</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    {{# if(d.houseStatus == 4){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="surrender">退租</a>
    {{# } }}
    {{# if(d.houseStatus == 5){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{# } }}
    {{# if(d.houseStatus == 6){ }}
    <a class="layui-btn layui-btn-success layui-btn-xs" lay-event="pass" style="background-color: #009688;">通过</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="reject">驳回</a>
    {{# } }}
    {{# if(d.houseStatus == 7){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancel">取消</a>
    {{# } }}
</script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/libs/layui/layui.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/common.js?v=318"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'upload', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var upload = layui.upload;
        var admin = layui.admin;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#landHome',
            url: '${pageContext.request.contextPath}/house/landHome',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                // {field: 'houseId', title: "houseId",align: 'center',sort: true},
                {field: 'houseName', title: '房名', align: 'center'},
                {field: 'landlordName', title: '房东', align: 'center'},
                {field: 'houseIdCard', title: '房产证号', align: 'center',minWidth: 100},
                {field: 'houseAddress', title: '房屋地址', align: 'center',minWidth: 100},
                {
                    field: 'houseStatus', title: '状态', templet: function (d) {
                        var strs = [
                            '<span class="text-success">已预订</span>',
                            '<span class="text-success">已租赁</span>',
                            '<span class="text-danger">已退租</span>',
                            '<span class="text-danger">申请退租</span>',
                            '<span class="text-success">未租赁</span>'
                        ];
                        return strs[d.houseStatus-3];
                    }, align: 'center',minWidth: 70
                },
                {field: 'housePrice', title: '租金(月)', align: 'center',minWidth: 70},
                {
                    field: 'houseType', title: '租赁类型', templet: function (d) {
                        var strs = [
                            '<span class="text-success">整租</span>',
                            '<span class="text-success">合租</span>'
                        ];
                        return strs[d.houseType-8];
                    }, align: 'center',minWidth: 50
                },
                {title: '操作', toolbar: '#landHomeTar', align: 'center', minWidth: 250}
            ]]
        });


        /* 表格搜索 */
        form.on('submit(searchLand)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(landHome)', function (obj) {
            var data = obj.data; // 获得当前行数据
            if (obj.event === 'check') { // 查看
                parent.location.href = "${pageContext.request.contextPath}/jsp/houseinfo.jsp?houseId="+data.houseId;
            } else if (obj.event === 'edit') { // 编辑
                admin.open({
                    type: 1,
                    title: '编辑房子',
                    area: ['500px'],
                    content: $('#houseEdit').html(),
                    success: function () {
                        // 回显表单数据
                        form.val('landHouseInfo', data);
                        var houseImg = "";
                        upload.render({
                            elem: '#houseImgs'
                            ,url: '${pageContext.request.contextPath}/house/houseimg'
                            ,multiple: true
                            ,before: function(obj){
                                obj.preview(function(index, file, result){
                                    $('#houseImg').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img" style="height: 70px">')
                                });
                            }
                            ,done: function(res){
                                if (res.code == 0){
                                    houseImg = houseImg + "," +res.msg;
                                    console.log(houseImg);
                                }
                            }
                        });
                        // 表单提交事件
                        form.on('submit(landHouseInfo)', function (data) {
                            data.field.houseImg = houseImg;
                            if (data.field.houseImg === ""){
                                layer.msg('未上传房屋图片');
                                return false;
                            }
                            $.ajax({
                                url:'${pageContext.request.contextPath}/house/updatehouse',
                                data: data.field,
                                dataType:'json',
                                type:'post',
                                success:function (res) {
                                    if(res.code == 0){
                                        layer.msg(res.msg,{icon:6},function () {
                                            houseImg = "";
                                            window.location.reload();
                                        })
                                    }else{
                                        layer.msg(res.msg,{icon:5});
                                    }
                                },
                                error:function () {
                                    layer.msg('请求失败');
                                }
                            })
                            return false;
                        });
                    }
                });
            }else if (obj.event === 'surrender') { // 退租
                layui.admin.confirm('确认退租吗？', function (index) {
                    $.ajax({
                        url:'${pageContext.request.contextPath}/house/surrenderland',
                        data: data,
                        dataType:'json',
                        type:'post',
                        success:function (res) {
                            if(res.code == 0){
                                layer.msg(res.msg,{icon:6},function () {
                                    window.location.reload();
                                })
                            }else{
                                layer.msg(res.msg,{icon:5});
                            }
                        },
                        error:function () {
                            layer.msg('请求失败');
                        }
                    })
                    layer.close(index);
                });
            }else if (obj.event === 'del') { // 删除
                layui.admin.confirm('确认删除吗？', function (index) {
                    $.ajax({
                        url:'${pageContext.request.contextPath}/house/delland',
                        data: data,
                        dataType:'json',
                        type:'post',
                        success:function (res) {
                            if(res.code == 0){
                                layer.msg(res.msg,{icon:6},function () {
                                    window.location.reload();
                                })
                            }else{
                                layer.msg(res.msg,{icon:5});
                            }
                        },
                        error:function () {
                            layer.msg('请求失败');
                        }
                    })
                    layer.close(index);
                });
            }else if (obj.event === 'pass') { // 通过
                $.ajax({
                    url:'${pageContext.request.contextPath}/house/passhome',
                    data: data,
                    dataType:'json',
                    type:'post',
                    success:function (res) {
                        if(res.code == 0){
                            layer.msg(res.msg,{icon:6},function () {
                                window.location.reload();
                            })
                        }else{
                            layer.msg(res.msg,{icon:5});
                        }
                    },
                    error:function () {
                        layer.msg('请求失败');
                    }
                })
            }else if (obj.event === 'reject') { // 驳回
                layui.admin.confirm('确认驳回吗？', function (index) {
                    $.ajax({
                        url:'${pageContext.request.contextPath}/house/rejecthome',
                        data: data,
                        dataType:'json',
                        type:'post',
                        success:function (res) {
                            if(res.code == 0){
                                layer.msg(res.msg,{icon:6},function () {
                                    window.location.reload();
                                })
                            }else{
                                layer.msg(res.msg,{icon:5});
                            }
                        },
                        error:function () {
                            layer.msg('请求失败');
                        }
                    })
                    layer.close(index);
                });
            }else if (obj.event === 'cancel') { // 取消
                layui.admin.confirm('确认取消吗？', function (index) {
                    $.ajax({
                        url:'${pageContext.request.contextPath}/house/delland',
                        data: data,
                        dataType:'json',
                        type:'post',
                        success:function (res) {
                            if(res.code == 0){
                                layer.msg(res.msg,{icon:6},function () {
                                    window.location.reload();
                                })
                            }else{
                                layer.msg(res.msg,{icon:5});
                            }
                        },
                        error:function () {
                            layer.msg('请求失败');
                        }
                    })
                    layer.close(index);
                });
            }
        });
    });
</script>
</body>
</html>
